package searchteam.client.ui.widgets;

import searchteam.client.beansMirrors.PaperMirror;
import searchteam.client.beansMirrors.PersonMirror;
import searchteam.client.ui.handlers.LinkClickHandler;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PaperMirrorDisplay extends MirrorDisplay{
	
	private PaperMirror bean;
	private Display display;
	private Button editButton;
	
	public PaperMirrorDisplay(Display d, PaperMirror mirror) {
		bean = mirror;
		display = d;
		this.setText(bean.getTitle());
	}

	public Panel display() {
		Grid panel = new Grid(5, 2);
		
		VerticalPanel authorsPanel = new VerticalPanel();
		
		panel.setWidget(0, 0, new Label("Titre : "));
		panel.setWidget(0, 1, new Label(bean.getTitle()));
		panel.setWidget(1, 0, new Label("Statut : "));
		panel.setWidget(1, 1, new Label(bean.getState()));
		panel.setWidget(2, 0, new Label("Auteurs : "));
		panel.setWidget(2, 1, authorsPanel);
		
		for(PersonMirror p : bean.getAuthors()) {
			PersonMirrorDisplay person = new PersonMirrorDisplay(display,p);
			authorsPanel.add(person);
			person.addClickHandler(new LinkClickHandler(display.getMainUI(), person));
		}
		
		editButton = new Button("Modifier");
		editButton.addClickHandler(new EditClickHandler(bean));
		
		panel.setWidget(4, 1, editButton);
		
		return panel;
	}

	public PaperMirror getBean() {
		return bean;
	}
	
	public int getId() {
		return bean.getId();
	}
	
	class EditClickHandler implements ClickHandler {
		
		private PaperMirror paper;
		
		public EditClickHandler(PaperMirror paper) {
			this.paper = paper;
		}

		public void onClick(ClickEvent event) {
			new UpdatePaperPopUp(display, paper);
		}
		
	}

}
